package com.wzx.user.controller;

import com.wzx.common.exception.LeadNewsException;
import com.wzx.common.pojo.resp.Result;
import com.wzx.user.dto.RedPkgDto;
import com.wzx.user.mongo.redis.HbMsg;
import com.wzx.user.pojo.RedPkg;
import com.wzx.user.service.mongo.RedPacketBasicService;
import com.wzx.user.vo.RedPkgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wzx
 * @version 1.0
 * @date 2022/8/12 - 18:51
 * @description 标题
 */
@Slf4j
@RestController
@RequestMapping("/redPacketBasic")
@Api(tags = "红包管理")
public class RedPacketBasicController {

    @Autowired
    private RedPacketBasicService redPacketBasicService;

    /*@ApiOperation(value = "抢红包")
    @GetMapping("/rob")
    public Result<RedPkgDto> rob(Integer hbId, Integer userId) throws LeadNewsException {
        log.error("用户：{}抢红包：{}，当前的时间是：{}", userId, hbId, System.currentTimeMillis());
        RedPkgDto rob = redPacketBasicService.rob(hbId, userId);
        log.error("用户：{}抢红包：{}，抢完 的时间是：{}", userId, hbId, System.currentTimeMillis());
        return Result.ok(rob);
    }*/

    @ApiOperation(value = "抢红包")
    @PostMapping("/rob")
    public Result<RedPkgDto> rob(@RequestBody RedPkgVo redPkgVo) throws LeadNewsException {
        RedPkgDto rob = redPacketBasicService.rob(redPkgVo);
        return Result.ok(rob);
    }

    @ApiOperation(value = "单个用户发红包后用户抢红包")
    @GetMapping("/robByUser")
    public Result robByUser(Integer hbId, Integer userId) throws LeadNewsException {
        redPacketBasicService.robByUser(hbId, userId);
        return Result.ok();
    }

    @PostMapping("/sendPkg")
    @ApiOperation(value = "发送红包返回红包id")
    public Result<Integer> sendPkg(@RequestBody HbMsg hbMsg) throws LeadNewsException {
        Integer b = redPacketBasicService.sendPkg(hbMsg);
        return Result.ok(b);
    }

    @PostMapping("/sendPkgByUserBalance")
    @ApiOperation(value = "根据用户余额发送红包，如果有余额直接发送即可")
    public Result<HbMsg> sendPkgByUserBalance(@RequestBody HbMsg hbMsg) throws LeadNewsException {
        HbMsg result = redPacketBasicService.sendPkgByUserBalance(hbMsg);
        return Result.ok(result);
    }

    @GetMapping("/queryRedPkgRobMessage")
    @ApiOperation(value = "查询抢红包信息")
    public Result queryRedPkgRobMessage(Integer hbId, Integer userId) throws Exception {
        Map<String, Object> maps = redPacketBasicService.queryRedPkgRobMessage(hbId, userId);
        return Result.ok(maps);
    }

    @GetMapping("/queryRedPkgById")
    @ApiOperation(value = "根据id查询红包信息")
    public Result<RedPkgDto> queryRedPkgById(Integer hbId) throws Exception {
        RedPkgDto redPkg = redPacketBasicService.queryRedPkgById(hbId);
        return Result.ok(redPkg);
    }

    @GetMapping("/queryUserIsRob")
    @ApiOperation(value = "查询用户是否已经抢过红包")
    public Result queryUserIsRob(Integer hbId, Integer userId) throws Exception {
        boolean b = redPacketBasicService.queryUserIsRob(hbId, userId);
        return Result.ok(b);
    }

    @PostMapping("/queryRedPkgByRecord")
    @ApiOperation(value = "查询用户红包记录")
    public Result<Map<String, Object>> queryRedPkgByRecord(@RequestBody RedPkgVo redPkgVo) throws Exception {
        Map<String, Object> map = redPacketBasicService.queryRedPkgByRecord(redPkgVo);
        return Result.ok(map);
    }

    @GetMapping("/robRedPkgByAllUsers")
    @ApiOperation(value = "查询用户红包记录")
    public Result<List<RedPkgDto>> robRedPkgByAllUsers(Integer hbId, String userIds) throws Exception {
        List<RedPkgDto> map = redPacketBasicService.robRedPkgByAllUsers(hbId, userIds);
        return Result.ok(map);
    }


}
